package com.tanhua.server.controller;

import cn.hutool.core.util.StrUtil;
import com.tanhua.server.service.QuanZiService;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.QuanZiVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;

@RestController
@RequestMapping("movements")
public class QuanZiController {
    @Autowired
    private QuanZiService quanZiService;

    // 查询好友动态
    @GetMapping
    public PageResult queryPublishList(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                       @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize)
    {
        return quanZiService.queryPublishList(page,pageSize);
    }


    // 发布动态
    @PostMapping
    public ResponseEntity<Void> savePublish(@RequestParam("textContent")String textContent,
                                            @RequestParam(value = "location", required = false) String location,
                                            @RequestParam(value = "latitude", required = false) String latitude,
                                            @RequestParam(value = "longitude", required = false) String longitude,
                                            @RequestParam(value = "imageContent", required = false) MultipartFile[] multipartFile){

        try {
            String publishId = this.quanZiService.savePublish(textContent, location, latitude, longitude, multipartFile);
            if (StrUtil.isNotEmpty(publishId)) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    // 查询推荐动态
    @GetMapping("recommend")
    public PageResult queryRecommendPublishList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return quanZiService.queryRecommendPublishList(page,pageSize);
    }


    // 点赞
    @GetMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id")String publishId){
        try {
            Long likeCount = quanZiService.likeComment(publishId);
            if (likeCount != null){
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    // 取消点赞
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Long> dislikeComment(@PathVariable("id")String publishId){
        try {
            Long likeCount = quanZiService.dislikeComment(publishId);
            if (likeCount != null){
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    // 喜欢
    @GetMapping("/{id}/love")
    public ResponseEntity<Long> loveComment(@PathVariable("id")String publishId){
        try {
            Long loveCount = quanZiService.loveComment(publishId);
            if (loveCount != null){
                return ResponseEntity.ok(loveCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    // 取消喜欢
    @GetMapping("/{id}/dislove")
    public ResponseEntity<Long> disloveComment(@PathVariable("id")String publishId){
        try {
            Long loveCount = quanZiService.disLoveComment(publishId);
            if (loveCount != null){
                return ResponseEntity.ok(loveCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    // 查询单条动态信息
    public ResponseEntity<QuanZiVo> queryById(@PathVariable("id")String publishId){
        try {
            QuanZiVo movements = quanZiService.queryById(publishId);
            if (null != movements){
                return ResponseEntity.ok(movements);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * TODO：谁看过我
     *
     * @return
     */
    @GetMapping("visitors")
    public ResponseEntity<Object> queryVisitors() {
        return ResponseEntity.ok(Collections.EMPTY_LIST);
    }
}
